// packageF/course/courseAll/courseList.js
const app = getApp();
import util from "../../../utils/util";
Page({

  /**
   * 页面的初始数据
   */
  data: {
    courseList:[], //章节列表
    keyword:'', //关键词
    page:1,
    lecturerId:'', //讲师ID
    isLoadMore: true,
    total_page: 0,
    timer:'',
    iPnoneBottomBol:false, //是否是苹果手机
    siteName:'', //商城名称
    imageUrl:'' //商城图片

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if(options.keyword) {
      this.keyword = options.keyword
    }
    this.getCourseList();
    this.getiPnoneBottom();
  },
  courseSearch(event) {
    this.setData({
      keyword:event.detail,
      page:1,
      courseList:[]
    })
    if (this.timer) clearTimeout(this.timer);
    this.timer = setTimeout(() => {
      util.debounce(this.getCourseList());
    }, 500);
  },
  getiPnoneBottom() {
    let that = this;
    try {
      let value = wx.getStorageSync('iPhoneBottom');
      if (value) {
        that.setData({
          iPnoneBottomBol: true
        });
        // Do something with return value
      }
    } catch (e) {
      // Do something when catch error
    }
  },
  getCourseList() {
    let params = {
      keyword:this.data.keyword,
      page: this.data.page,
      lecturerId: this.data.lecturerId,
      basic_info:1
    }
    let urlStr = app.getNetAddresss("plugin.course-supply.api.course.index");
    app._postNetWork({
      url: urlStr,
      data: params,
      success: (resdata) => {
        let res = resdata.data;
        let list = res.data.data;
        let basicInfo =  res.basic_info;
        wx.hideLoading();
        wx.hideNavigationBarLoading();//隐藏导航条加载动画
        //停止下拉刷新
        wx.stopPullDownRefresh();
        if (res.result == 1) {
          this.data.isLoadMore = true;
          this.data.totalPage = res.data.last_page;
          if (!this.data.totalPage) {
            this.data.totalPage = 0;
          }
          this.setData({
            courseList : list,
            siteName:basicInfo?.home?.mailInfo?.name,
            imageUrl:basicInfo?.home?.mailInfo?.logo_url,
          })
        } else {
          this.data.page = this.data.page - 1;
          this.isLoadMore = false;
          wx.showToast({
            icon: 'none',
            title: res.msg,
            duration: 1500
          });
        }
      },
      fail: function (res) {
        console.log(res);
      }
    });
  },
  getMoreData() {
    this.data.isLoadMore = false; // 防止多次请求分页数据
    if (this.data.page >= this.data.totalPage) {
      return;
    } else {
      this.data.page = this.data.page + 1;
      let urlStr = app.getNetAddresss("plugin.course-supply.api.course.index");
      let params = {
        keyword:this.data.keyword,
        page: this.data.page,
        lecturerId: this.data.lecturerId
      }
      app._getNetWork({
        url: urlStr,
        data: params,
        success: (resdata) => {
          let res = resdata.data;
          let list = res.data.data;
          if (res.result == 1) {
            this.data.isLoadMore = true;
            this.setData({
              courseList : this.data.courseList.concat(list),
            })
          } else {
            this.data.page = this.data.page - 1;
            this.isLoadMore = false;
            wx.showToast({
              icon: 'none',
              title: res.msg,
              duration: 1500
            });
          }
        },
        fail: function (res) {
          console.log(res);
        }
      });
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    wx.showNavigationBarLoading();
     //显示 loading 提示框。需主动调用 wx.hideLoading 才能关闭提示框
     wx.showLoading({
      title: '刷新中...',
    })
    this.setData({
      page:1
    })
    this.getCourseList();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    if (this.data.isLoadMore) {
      this.getMoreData();
    } else {
      console.log('没有更多了');
    }
  },
/**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    return {
      title: '课程供应链-' + this.data.siteName,
      imageUrl:this.data.imageUrl,
      path: `/packageF/course/courseAll/courseList?keyword=${this.data.keyword}`
    };
  },
  onShareTimeline() {
    return {
      title: '课程供应链-' + this.data.siteName,
      imageUrl:this.data.imageUrl,
      path: `/packageF/course/courseAll/courseList?keyword=${this.data.keyword}`
    };
  }
})